$(function(){
	
		var arr2=[];
		var str1 = $.cookie("obj"); 
		var obj2 = JSON.parse(str1);//加载判断cookie 是否为空

		if(obj2.data.length==0){
			$(".car-empty").css({"display":"block"});
			$(".container").css({"display":"none"});
		}else{
			$(".container").css({"display":"block"});
			
			angular.module('app', []).controller('firstController',
				function($scope) {
					var str1 = $.cookie("obj"); 
					var obj2 = JSON.parse(str1);　
					
					for(var k in obj2.data){
						arr2.push({
							src: obj2.data[k].src,
							title:obj2.data[k].title ,
							quantity: obj2.data[k].quantity, 
							price:obj2.data[k].price,
							zhekou:obj2.data[k].zhekou
						})
					};

					$scope.Product = arr2;
					

					
							//计算总额
					$scope.totalPrice = function(index) {
						var total = 0;

						angular.forEach($scope.Product, function(item) {
								total += item.quantity * item.price;
						});
						return total;	

					}
		   					//计算总数量
					$scope.totalQuantity = function() {
						var total = 0;
						angular.forEach($scope.Product, function(item) {
						total += parseInt(item.quantity);
						});
						return total;
					}
						//移除选中的一个
					$scope.remove = function(index) {
						$scope.Product.splice(index, 1);
						
						if(arr2.length==0){
							$(".car-empty").css({"display":"block"});
							$(".container").css({"display":"none"});
						}
						var  obj= { data:arr2};
						var str = JSON.stringify(obj);　　
						$.cookie("obj", str, { 
						    expires:7          
						});

					}
						//移除所有
					$scope.removeall = function() {
						var index;
						for(index = $scope.Product.length - 1; index >= 0; index--) {
							$scope.remove(index);
						}
						if(arr2.length==0){
							$(".car-empty").css({"display":"block"});
							$(".container").css({"display":"none"});
						}
						var  obj= { data:arr2};
						var str = JSON.stringify(obj);　　
						$.cookie("obj", str, { 
						    expires:7          
						});
					}
		
					$scope.reduce = function(index) {
						if($scope.Product[index].quantity != 1) {
							$scope.Product[index].quantity--;
						} else {
							var ans = confirm("是否移除该产品？");
						if(ans) {
							$scope.remove(index);
						} else {
							$scope.Product[index].quantity = 1;
						}
						}
					}
		
					$scope.add = function(index) {
						$scope.Product[index].quantity++;
					}
						
					$scope.$watch('Product', function(newValue, oldValue) {
						angular.forEach(newValue, function(item, key) {
							if(item.quantity < 1) {
								var ans = confirm("是否移除该产品？");
							if(ans) {
								$scope.remove(key);
							} else {
								item.quantity = oldValue[key].quantity;
							}
								return;
							}
						});
					}, true);
		
				}
			);			
			　
		}
		　

})
			